<cfparam name="form.cid" default="0">
<cfif formaction IS 'EDIT'>
	<cfinclude template="cleanup.cfm">
</cfif>
<cfset blog_filename = replace(b_title, " ", "-", "ALL")>
<cfset blog_filename = replace(blog_filename, "/", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "\", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "*", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "=", "", "ALL")>
<cfset blog_filename = replace(blog_filename, ".", "", "ALL")>
<cfset blog_filename = replace(blog_filename, ",", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "`", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "+", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "~", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "?", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "!", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "##", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "%", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "(", "", "ALL")>
<cfset blog_filename = replace(blog_filename, ")", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "%", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "^", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "$", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "@", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "<", "", "ALL")>
<cfset blog_filename = replace(blog_filename, ">", "", "ALL")>
<cfset blog_filename = replace(blog_filename, ".", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "?", "", "ALL")>
<cfset blog_filename = replace(blog_filename, "'", "", "ALL")>
<cfset blog_filename = replace(blog_filename, '"', "", "ALL")>
<cfset blog_filename = replace(blog_filename, '|', "", "ALL")>
<cfset blog_filename = replace(blog_filename, '_', "-", "ALL")>
<cfset blog_filename = blog_filename & '.cfm'>
<cfset blog_filename = lcase(blog_filename)>

<cfset b_year = dateformat(b_postedon, "yyyy")>
<cfset b_month = dateformat(b_postedon, "m")>
<cfset b_day = dateformat(b_postedon, "d")>
<cfif NOT directoryexists("#request.basepath#blog/#b_year#")>
	<cfdirectory action="create" directory="#request.basepath#blog/#b_year#">
</cfif>
<cfif NOT directoryexists("#request.basepath#blog/#b_year#/#b_month#")>
	<cfdirectory action="create" directory="#request.basepath#blog/#b_year#/#b_month#">
</cfif>
<cfif NOT directoryexists("#request.basepath#blog/#b_year#/#b_month#/#b_day#")>
	<cfdirectory action="create" directory="#request.basepath#blog/#b_year#/#b_month#/#b_day#">
</cfif>

<cfif formaction IS 'ADD'>
	<cfif fileexists('#request.basepath#blog/#b_year#/#b_month#/#b_day#/#blog_filename#')>
        <cfset updatecount = 1>
        <cfset dupfile = 1>
       <cfloop condition = "dupfile GREATER THAN 0">
            <cfset new_filename = '#blog_filename#_update_#updatecount#'>
            <cfset new_b_title = '#b_title# (update #updatecount#)'>
            <cfif NOT fileexists('#request.basepath#blog/#b_year#/#b_month#/#b_day#/#new_filename#')>
                <cfset dupfile = 0>
            </cfif>
            <cfset updatecount = updatecount + 1>
        </cfloop>
        <cfset blog_filename = new_filename>
        <cfset form.b_title = new_b_title>
    </cfif>
</cfif>

<cfset blog_postdate = "#dateformat(b_postedon, 'mm/dd/yyyy')# #b_hour#:#b_minute# #b_ampm#">

<cfif formaction IS 'ADD'>    
    <cftransaction>
    <cfquery name="insertblogentry" datasource="#request.dsn#">
        INSERT INTO blog_posts
        (b_title, b_file, b_month, b_year, b_day, b_postedon, b_brief, status, b_content, categories)
        VALUES
        ('#form.b_title#', '#blog_filename#', #b_month#, #b_year#, #b_day#, #createodbcdatetime(blog_postdate)#, '#form.b_brief#', 1, '#form.b_content#', '#form.cid#')
     </cfquery>

    <cfquery name = "qID" datasource="#request.dsn#">
    SELECT LAST_INSERT_ID() AS newid
    </cfquery>
	
    <cfset b_id = qID.newid>
	</cftransaction>
</cfif>
<cfif formaction IS 'EDIT'>
	<cfquery name="updateentry" datasource="#request.dsn#">
		UPDATE blog_posts
		SET b_title='#form.b_title#', 
		b_file='#blog_filename#', 
		b_month=#b_month#, 
		b_year=#b_year#, 
		b_day=#b_day#, 
		b_postedon=#createodbcdatetime(blog_postdate)#, 
		b_brief='#form.b_brief#',
		b_content = '#form.b_content#',
        categories = '#form.cid#'
		WHERE b_id = <cfqueryparam value="#b_id#" cfsqltype="cf_sql_integer">
	 </cfquery>     
</cfif>

<cfset catstring = "">
<cfloop list="#form.cid#" delimiters="," index="catid">
	<cfquery name="qcat" datasource="#request.dsn#">
    SELECT * FROM blog_categories
    WHERE id = <cfqueryparam value="#catid#" cfsqltype="cf_sql_integer">
    </cfquery>
    
    <cfset catstring = catstring & '<a href = "#request.absolutepath#index.cfm?c=blog&a=category&cid=#catid#">#qcat.cname#</a>&nbsp;'>
</cfloop>

<cfinclude template="insertintocategories.cfm">
<cfinclude template="insertmetadata.cfm">
<cfinclude template="inserttags.cfm">

<cfsavecontent variable="b_content">
<cfoutput>
<div id="blog_pad1">
<div id="blog_item">
<div id="blog_headlines">#b_title#</div>
<div id="blog_date">Posted on #DayofWeekAsString(DayOfWeek(b_postedon))#, #MonthAsString(Month(b_postedon))# #Day(b_postedon)#, #Year(b_postedon)# at #b_hour#:#b_minute# #b_ampm#</div>
<div id="blog_categories">Posted in:&nbsp;&nbsp;#catstring#</div>
<cfif len(trim(b_author)) GT 0><div id="blog_author">Posted by: #b_author#</div></cfif>
<div id="blog_entry">#b_content#</div>
<div id="blog_clear"></div>
</div>
</div>
</cfoutput>
</cfsavecontent>

<cffile action="write" output="#b_content#" file="#request.basepath#blog/#b_year#/#b_month#/#b_day#/#blog_filename#"> 

 
<cfif formaction IS 'ADD'>
	<cfinvoke component="cfcs.common.shared" method="redirect" url="index.cfm?c=blog&a=default&reinit=1" msg="New Post was Added" />
</cfif>
<cfif formaction IS 'EDIT'>
	<cfinvoke component="cfcs.common.shared" method="redirect" url="index.cfm?c=blog&a=default&reinit=1" msg="Post was Saved" />
</cfif>    

